package com.androidhuman.example.mapsv2example;

import java.util.ArrayList;

import com.androidhuman.example.mapsv2example.ListActivity.DataAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View.OnClickListener;

public class MeActivity extends FragmentActivity {

	SharedPreferences userPrefs;
	SharedPreferences.Editor userPrefsEditor;

	GPSTracker gps;
	View btnUpdateLocation;
	
	TextView LocationView;
	TextView EmailView;
	TextView NicknameView;
	TextView SexView;
	TextView IntroduceView;
	
	
	
	TextView text;

	static Context mContext;

	private ListView listview;
	private MDataAdapter adapter;
	private LayoutInflater mInflater;
	private ArrayList<MData> alist;
	private Context context;
	
	int savedsex;
	String savednickname;
	String savedemail;
	String savedintroduce;
	String savedlocation;
	
	LinearLayout logout_btn;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		userPrefs = getSharedPreferences("USER", MODE_PRIVATE);
		userPrefsEditor = userPrefs.edit(); 
		
		setContentView(R.layout.activity_me);
		
		savedlocation = userPrefs.getString("mylocation","업데이트 되지 않음"); 
		savedsex = userPrefs.getInt("mysex", 0);
		savednickname = userPrefs.getString("mynickname", "-");
		savedemail = userPrefs.getString("myemail", "-");
		savedintroduce = userPrefs.getString("myintroduce", "-");
		
		
		

		View btn_close = (View) findViewById(R.id.bt_close);
		btn_close.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		mContext = getApplicationContext();

		listview = (ListView) findViewById(R.id.list_me);
		alist = new ArrayList<MData>();
		adapter = new MDataAdapter(this, alist);
		listview.setAdapter(adapter);
		////////////////////////////////////////////////////////////////
		
		
		btnUpdateLocation=(View) findViewById(R.id.btn_update);
		
		NicknameView = (TextView) findViewById(R.id.me_nickname);
		EmailView = (TextView) findViewById(R.id.me_email);
		SexView = (TextView) findViewById(R.id.me_sex);
		IntroduceView = (TextView) findViewById(R.id.me_introduce);
		
		LocationView = (TextView) findViewById(R.id.me_location);
		
		logout_btn = (LinearLayout) findViewById(R.id.logout_btn);
	
		if(savedsex==0){
			SexView.setText("성별 모름");
		}else if(savedsex==1){
			SexView.setText("여성");
			
		}else{
			SexView.setText("남성");
		}
	
		NicknameView.setText(savednickname);
		EmailView.setText(savedemail);
		LocationView.setText(savedlocation);
		IntroduceView.setText(savedintroduce);
		
		
		
		
		////////////////////////////////////////////////////////////////

		context = this;

		adapter.insert(new MData(this, 3, "활동로그 샘플", "김제보"), 0);


		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View arg1,
					int position, long arg3) {

//				Toast.makeText(context, Integer.toString(position),
//						Toast.LENGTH_SHORT).show();

			}
		});

		btnUpdateLocation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// create class object
				gps = new GPSTracker(MeActivity.this);
				Log.i("Socialism", "OK0");
				// check if GPS enabled
				if (gps.canGetLocation()) {

					double latitude = gps.getLatitude();
					double longitude = gps.getLongitude();

					// \n is for new line
					Toast.makeText(
							getApplicationContext(),
							"현재 좌표 - \n위도: " + latitude
									+ "\n경도: " + longitude, Toast.LENGTH_LONG)
							.show();
					
					userPrefsEditor.putFloat("mylatitude", (float)latitude); 
					userPrefsEditor.putFloat("mylongitude", (float)longitude); 
					// Toast.makeText(getApplicationContext(),
					// gps.address,
					// Toast.LENGTH_LONG).show();
					// Log.i("Socialism","OK0");
					if(gps.getAddress(latitude, longitude)==null){
						LocationView.setText("업데이트 되지 않음");
					}else{
					LocationView.setText(gps.getAddress(latitude, longitude));
					}
				} else {
					// can't get location
					// GPS or Network is not enabled
					// Ask user to enable GPS/network in settings
					gps.showSettingsAlert();
				}
				// gps.getAddress();
				
				
				savedlocation = LocationView.getText().toString();
				
			
				userPrefsEditor.putString("mylocation", savedlocation); 
				userPrefsEditor.commit();	
			
				
			

				
			}
		});
		
		logout_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
			
				
				
				Logout();
		
			

				
			}
		});
		
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		return true;
	}
	
	public void Logout(){
		
		userPrefsEditor.putInt("myid", 0);
		userPrefsEditor.commit();
		userPrefsEditor.putInt("mysex", 0);
		userPrefsEditor.commit();
		userPrefsEditor.putString("mynickname", null);
		userPrefsEditor.commit();
		userPrefsEditor.putString("myemail", null);
		userPrefsEditor.commit();
		userPrefsEditor.putString("mylocation", null);
		userPrefsEditor.commit();
		userPrefsEditor.putInt("check", 0);
		userPrefsEditor.commit();
		
		startActivity(new Intent(getApplicationContext(), IntroActivity.class));
		
	}



	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		// if (id == R.id.action_settings) {
		// return true;
		// }
		return super.onOptionsItemSelected(item);
	}

	class MDataAdapter extends ArrayAdapter<MData> {
		private LayoutInflater mInflater;

		public MDataAdapter(Context context, ArrayList<MData> object) {
			super(context, 0, object);

			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		}

		public View getView(int position, View v, ViewGroup parent) {
			View view = null;
			if (v == null) {
				view = mInflater.inflate(R.layout.me_custom, null);
			} else {
				view = v;
			}
			final MData data = this.getItem(position);
			if (data != null) {

				text = (TextView) view.findViewById(R.id.mylog_text);

				text.setText(data.getText());

			}
			return view;
		}
	}

	private class MData {

		private int Type;
		private String Text;
		private String Time;

		public MData(Context context, int mType, String mText, String mTime) {

			Type = mType;
			Text = mText;

		}

		public int getType() {
			return Type;
		}

		public String getText() {
			return Text;
		}

	}

}
